package com.useeinfo.oa.modules.task.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.task.entity.TaskAuditOrder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("TaskAuditOrderDao")
public class TaskAuditOrderDao implements CrudDao<TaskAuditOrder> {
    private final static Logger logger = LoggerFactory.getLogger(TaskAuditOrderDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("i.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }
        String taskOrderId = queryHash.get("taskOrderId");
        if (!StringUtils.isEmpty(taskOrderId)) {
            conditionHash.put("i.taskOrder.id = ?{paramIndex} ", StringConverters.null2Long(taskOrderId));
        }
        String orderName = queryHash.get("orderName");
        if (!StringUtils.isEmpty(orderName)) {
            conditionHash.put("(i.taskOrder.orderName like ?{paramIndex}) ", "%"+orderName+"%");
        }
        String serial = queryHash.get("serial");
        if (!StringUtils.isEmpty(serial)) {
            conditionHash.put("(i.taskOrder.serial like ?{paramIndex}) ", "%"+serial+"%");
        }
        String startTime = queryHash.get("startTime");
        if (!StringUtils.isEmpty(startTime)) {
            conditionHash.put("(i.createDate >= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",startTime));
        }
        String endTime = queryHash.get("endTime");
        if (!StringUtils.isEmpty(endTime)) {
            conditionHash.put("(i.createDate <= ?{paramIndex}) ", DateTimeUtils.parse("yyyy-MM-dd HH:mm",endTime));
        }
        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(i.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        String auditUserId = queryHash.get("auditUserId");
        if (!StringUtils.isEmpty(auditUserId)) {
            conditionHash.put("(i.auditUser.id = ?{paramIndex}) ", StringConverters.null2Long(auditUserId));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(i) from TaskAuditOrder i ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<TaskAuditOrder> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select i from TaskAuditOrder i ", "order by i.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<TaskAuditOrder> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, TaskAuditOrder.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public TaskAuditOrder findModel(Long id) {
        return entityManager.find(TaskAuditOrder.class, id);
    }

    @Override
    public Integer add(TaskAuditOrder model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("TaskAuditOrderImpl添加TaskAuditOrder成功！");
        return 1;
    }

    @Override
    public Integer update(TaskAuditOrder model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        TaskAuditOrder insAuditOrder = entityManager.find(TaskAuditOrder.class, id);
        entityManager.remove(insAuditOrder);
        return 1;
    }

    @Override
    public Integer deleteLogic(TaskAuditOrder model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
